<?php
$site		 = $this->site;
$employes	 = $this->employes;
$employeList	 = array();
$codes		 = array();
$codec		 = array();
foreach ($this->codes as $code) {
    $codec[$code->id]	 = $code->code;
    $codes[]		 = $code;
}
foreach ($employes as $employe)
    $employeList[]	 = $employe;
?>

<div class='content-head'>
    <form id='tpl-empty' action='<?php echo $this->url('RHX/default', array('controller'	 => 'template', 'action'	 => 'savetemplateempty')) ?>' method='post'>
	<?php $id		 = 0 ?>
	<label style='padding: auto 5px;'>
	    Libellé du Planning : <input type='text' name='libelle' value='Planning Type <?php echo $site->libelle ?>' size="70" style='border: solid 1px #f63700'>
	</label>
	<input type='hidden' name='site' value='<?php echo $site->id ?>'>
	<?php foreach ($employeList as $employe): ?>
    	<input type='hidden' id='employe-<?php echo $id++ ?>' name='employe[<?php echo $employe->matricule ?>]' value='<?php echo $employe->matricule ?>'>
    	<input type='hidden' id='lundi-<?php echo $employe->matricule ?>' name='lundi-<?php echo $employe->matricule ?>' value='3'>
    	<input type='hidden' id='lundi-<?php echo $employe->matricule ?>_entree' name='lundi-<?php echo $employe->matricule ?>_entree' value='00:00'>
    	<input type='hidden' id='lundi-<?php echo $employe->matricule ?>_sortie' name='lundi-<?php echo $employe->matricule ?>_sortie' value='00:00'>

    	<input type='hidden' id='mardi-<?php echo $employe->matricule ?>' name='mardi-<?php echo $employe->matricule ?>' value='3'>
    	<input type='hidden' id='mardi-<?php echo $employe->matricule ?>_entree' name='mardi-<?php echo $employe->matricule ?>_entree' value='00:00'>
    	<input type='hidden' id='mardi-<?php echo $employe->matricule ?>_sortie' name='mardi-<?php echo $employe->matricule ?>_sortie' value='00:00'>

    	<input type='hidden' id='mercredi-<?php echo $employe->matricule ?>' name='mercredi-<?php echo $employe->matricule ?>' value='3'>
    	<input type='hidden' id='mercredi-<?php echo $employe->matricule ?>_entree' name='mercredi-<?php echo $employe->matricule ?>_entree' value='00:00'>
    	<input type='hidden' id='mercredi-<?php echo $employe->matricule ?>_sortie' name='mercredi-<?php echo $employe->matricule ?>_sortie' value='00:00'>

    	<input type='hidden' id='jeudi-<?php echo $employe->matricule ?>' name='jeudi-<?php echo $employe->matricule ?>' value='3'>
    	<input type='hidden' id='jeudi-<?php echo $employe->matricule ?>_entree' name='jeudi-<?php echo $employe->matricule ?>_entree' value='00:00'>
    	<input type='hidden' id='jeudi-<?php echo $employe->matricule ?>_sortie' name='jeudi-<?php echo $employe->matricule ?>_sortie' value='00:00'>

    	<input type='hidden' id='vendredi-<?php echo $employe->matricule ?>' name='vendredi-<?php echo $employe->matricule ?>' value='3'>
    	<input type='hidden' id='vendredi-<?php echo $employe->matricule ?>_entree' name='vendredi-<?php echo $employe->matricule ?>_entree' value='00:00'>
    	<input type='hidden' id='vendredi-<?php echo $employe->matricule ?>_sortie' name='vendredi-<?php echo $employe->matricule ?>_sortie' value='00:00'>

    	<input type='hidden' id='samedi-<?php echo $employe->matricule ?>' name='samedi-<?php echo $employe->matricule ?>' value='3'>
    	<input type='hidden' id='samedi-<?php echo $employe->matricule ?>_entree' name='samedi-<?php echo $employe->matricule ?>_entree' value='00:00'>
    	<input type='hidden' id='samedi-<?php echo $employe->matricule ?>_sortie' name='samedi-<?php echo $employe->matricule ?>_sortie' value='00:00'>

    	<input type='hidden' id='dimanche-<?php echo $employe->matricule ?>' name='dimanche-<?php echo $employe->matricule ?>' value='3'>
    	<input type='hidden' id='dimanche-<?php echo $employe->matricule ?>_entree' name='dimanche-<?php echo $employe->matricule ?>_entree' value='00:00'>
    	<input type='hidden' id='dimanche-<?php echo $employe->matricule ?>_sortie' name='dimanche-<?php echo $employe->matricule ?>_sortie' value='00:00'>
	<?php endforeach ?>
    </form>
    <div class='toolbar'>
	<button type='button' class='btn btn-success' onclick='$("#tpl-empty").submit();'>
	    Enregistrer
	</button>
	<button type='button' class='btn btn-inverse' onclick='$("#pl-dialog").dialog("close");
		return false;'>
	    Annuler
	</button>
    </div>
</div>

<div class='content-body'>
    <div class="list-container">
	<table class="pl-table" style='width: 100%'>
	    <thead>
		<tr>
		    <td style='text-align: center' rowspan='2'>
			<span>Mat.</span>
		    </td>
		    <td style='text-align: center' rowspan='2'>
			<span>Poste</span>
		    </td>
		    <td style='text-align: center' rowspan='2'>
			<span>Nom</span>
		    </td>
		    <td style='text-align: center' colspan='3'>
			Lundi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Mardi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Mercredi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Jeudi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Vendredi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Samedi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Dimanche
		    </td>
		</tr>
		<tr style='font-size: 0.7em'>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		</tr>
	    </thead>
	    <tbody style="font-size: 0.9em; text-align: center; width: 100%">
		<?php
		$jaune	 = 1;
		$pk	 = 0;
		?>
		<?php foreach ($employeList as $employe): ?>
    	    <td class='<?php echo (($jaune % 2) == 0) ? 'color-jaune2' : 'color-jaune1' ?>'>
		    <?php echo $employe->matricule ?>
    	    </td>
    	    <td class='<?php echo (($jaune % 2) == 0) ? 'color-jaune2' : 'color-jaune1' ?>'>
		    <?php $poste = $employe->getPoste(); ?>
		    <?php echo $poste->code; ?>
    	    </td>
    	    <td class='<?php echo (($jaune % 2) == 0) ? 'color-jaune2' : 'color-jaune1' ?>'>
		    <?php echo $employe->prenom ?>
    	    </td>

    	    <!-- Lundi -->
    	    <td class='color-OFF' id='tpl-<?php echo $pk ?>-lundi' style='position: relative;'>
    		<a href='#' class='full-link pl-day-edit code_activite' data-id='<?php echo $employe->matricule ?>' data-prefix='lundi' data-pk='<?php echo $pk ?>'>
    		    OFF
    		</a>
    	    </td>
    	    <td class='color-OFF' id="tpl-<?php echo $pk ?>-lundi-entree">
    		<a href='#' class='full-link pl-day-edit entree' data-id='<?php echo $employe->matricule ?>' data-prefix='lundi' data-pk='<?php echo $pk ?>'>
    		    00:00
    		</a>
    	    </td>
    	    <td class='color-OFF' id="tpl-<?php echo $pk ?>-lundi-sortie">
    		<a href='#' class="full-link pl-day-edit sortie" data-id='<?php echo $employe->matricule ?>' data-prefix='lundi' data-pk='<?php echo $pk ?>'>
    		    00:00
    		</a>
    	    </td>

    	    <!-- Mardi -->
    	    <td class='color-OFF' id='tpl-<?php echo $pk ?>-mardi' style='position: relative;'>
    		<a href='#' class='full-link pl-day-edit code_activite' data-id='<?php echo $employe->matricule ?>' data-prefix='mardi' data-pk='<?php echo $pk ?>'>
    		    OFF
    		</a>
    	    </td>
    	    <td class='color-OFF' id="tpl-<?php echo $pk ?>-mardi-entree">
    		<a href='#' class='full-link pl-day-edit entree' data-id='<?php echo $employe->matricule ?>' data-prefix='mardi' data-pk='<?php echo $pk ?>'>
    		    00:00
    		</a>
    	    </td>
    	    <td class='color-OFF' id="tpl-<?php echo $pk ?>-mardi-sortie">
    		<a href='#' class="full-link pl-day-edit sortie" data-id='<?php echo $employe->matricule ?>' data-prefix='mardi' data-pk='<?php echo $pk ?>'>
    		    00:00
    		</a>
    	    </td>

    	    <!-- Mercredi -->
    	    <td class='color-OFF' id='tpl-<?php echo $pk ?>-mercredi' style='position: relative;'>
    		<a href='#' class='full-link pl-day-edit code_activite' data-id='<?php echo $employe->matricule ?>' data-prefix='mercredi' data-pk='<?php echo $pk ?>'>
    		    OFF
    		</a>
    	    </td>
    	    <td class='color-OFF' id="tpl-<?php echo $pk ?>-mercredi-entree">
    		<a href='#' class='full-link pl-day-edit entree' data-id='<?php echo $employe->matricule ?>' data-prefix='mercredi' data-pk='<?php echo $pk ?>'>
    		    00:00
    		</a>
    	    </td>
    	    <td class='color-OFF' id="tpl-<?php echo $pk ?>-mercredi-sortie">
    		<a href='#' class="full-link pl-day-edit sortie" data-id='<?php echo $employe->matricule ?>' data-prefix='mercredi' data-pk='<?php echo $pk ?>'>
    		    00:00
    		</a>
    	    </td>

    	    <!-- Jeudi -->
    	    <td class='color-OFF' id='tpl-<?php echo $pk ?>-jeudi' style='position: relative;'>
    		<a href='#' class='full-link pl-day-edit code_activite' data-id='<?php echo $employe->matricule ?>' data-prefix='jeudi' data-pk='<?php echo $pk ?>'>
    		    OFF
    		</a>
    	    </td>
    	    <td class='color-OFF' id="tpl-<?php echo $pk ?>-jeudi-entree">
    		<a href='#' class='full-link pl-day-edit entree' data-id='<?php echo $employe->matricule ?>' data-prefix='jeudi' data-pk='<?php echo $pk ?>'>
    		    00:00
    		</a>
    	    </td>
    	    <td class='color-OFF' id="tpl-<?php echo $pk ?>-jeudi-sortie">
    		<a href='#' class="full-link pl-day-edit sortie" data-id='<?php echo $employe->matricule ?>' data-prefix='jeudi' data-pk='<?php echo $pk ?>'>
    		    00:00
    		</a>
    	    </td>

    	    <!-- Vendredi -->
    	    <td class='color-OFF' id='tpl-<?php echo $pk ?>-vendredi' style='position: relative;'>
    		<a href='#' class='full-link pl-day-edit code_activite' data-id='<?php echo $employe->matricule ?>' data-prefix='vendredi' data-pk='<?php echo $pk ?>'>
    		    OFF
    		</a>
    	    </td>
    	    <td class='color-OFF' id="tpl-<?php echo $pk ?>-vendredi-entree">
    		<a href='#' class='full-link pl-day-edit entree' data-id='<?php echo $employe->matricule ?>' data-prefix='vendredi' data-pk='<?php echo $pk ?>'>
    		    00:00
    		</a>
    	    </td>
    	    <td class='color-OFF' id="tpl-<?php echo $pk ?>-vendredi-sortie">
    		<a href='#' class="full-link pl-day-edit sortie" data-id='<?php echo $employe->matricule ?>' data-prefix='vendredi' data-pk='<?php echo $pk ?>'>
    		    00:00
    		</a>
    	    </td>

    	    <!-- Samedi -->
    	    <td class='color-OFF' id='tpl-<?php echo $pk ?>-samedi' style='position: relative;'>
    		<a href='#' class='full-link pl-day-edit code_activite' data-id='<?php echo $employe->matricule ?>' data-prefix='samedi' data-pk='<?php echo $pk ?>'>
    		    OFF
    		</a>
    	    </td>
    	    <td class='color-OFF' id="tpl-<?php echo $pk ?>-samedi-entree">
    		<a href='#' class='full-link pl-day-edit entree' data-id='<?php echo $employe->matricule ?>' data-prefix='samedi' data-pk='<?php echo $pk ?>'>
    		    00:00
    		</a>
    	    </td>
    	    <td class='color-OFF' id="tpl-<?php echo $pk ?>-samedi-sortie">
    		<a href='#' class="full-link pl-day-edit sortie" data-id='<?php echo $employe->matricule ?>' data-prefix='samedi' data-pk='<?php echo $pk ?>'>
    		    00:00
    		</a>
    	    </td>

    	    <!-- Dimanche -->
    	    <td class='color-OFF' id='tpl-<?php echo $pk ?>-dimanche' style='position: relative;'>
    		<a href='#' class='full-link pl-day-edit code_activite' data-id='<?php echo $employe->matricule ?>' data-prefix='dimanche' data-pk='<?php echo $pk ?>'>
    		    OFF
    		</a>
    	    </td>
    	    <td class='color-OFF' id="tpl-<?php echo $pk ?>-dimanche-entree">
    		<a href='#' class='full-link pl-day-edit entree' data-id='<?php echo $employe->matricule ?>' data-prefix='dimanche' data-pk='<?php echo $pk ?>'>
    		    00:00
    		</a>
    	    </td>
    	    <td class='color-OFF' id="tpl-<?php echo $pk ?>-dimanche-sortie">
    		<a href='#' class="full-link pl-day-edit sortie" data-id='<?php echo $employe->matricule ?>' data-prefix='dimanche' data-pk='<?php echo $pk ?>'>
    		    00:00
    		</a>
    	    </td>

    	    </tr>
		<?php
		$jaune++;
		$pk++;
		?>
	    <?php endforeach; ?>
	    </tbody>
	</table>
    </div>
</div>

<div styl='display: none'>

</div>

<script type="text/javascript">
	    // Codecs
	    var codec = new Array();
<?php foreach ($codes as $c): ?>
    	    codec['<?php echo $c->code; ?>'] = <?php echo $c->id; ?>;
    	    codec[<?php echo $c->id; ?>] = '<?php echo $c->code; ?>';
<?php endforeach; ?>
	    // Initialisation des liens d'édition
	    var openTemplate = '';
	    $(document).ready(function() {
		// pl-day-edit
		$('.pl-day-edit').click(function(event) {
		    event.preventDefault();
		    // Variables
		    var prefix = $(this).attr('data-prefix');
		    var pk = $(this).attr('data-pk');	// Identifiant unique pour chaque ligne
		    var id = $(this).attr('data-id'); //

		    // Position de la souris
		    x = event.pageX;
		    y = event.pageY;
		    // Taille de l'écran
		    sw = $(window).width();
		    sh = $(window).height();
		    // Taille du dialogue
		    dw = 500;
		    dh = 400;
		    // Positionnement du dialogue
		    px = 0;
		    py = 0;
		    // Test sur la largeur
		    if ((sw - (dw + x)) > 0) {
			px = x;
		    } else {
			px = x - dw;
		    }
		    // Test sur la hauteur
		    if ((sh - y) >= dh - 250) {
			py = y - 40;
		    } else {
			py = y - dh + 80;
		    }
		    // Suppression des autres DIV conteneur
		    $('.rhPopover').remove();
		    // Ajout d'un div conteneur : tpl-0-lundi
		    //alert('#tpl-' + pk + '-' + prefix);
		    $('#tpl-' + pk + '-' + prefix).append("<div class='rhPopover'></div>");
		    // Chargement du contenu
		    ajax = $.ajax({
			type: "post",
			url: "<?php echo $this->url('RHX/default', array('controller'	 => 'template', 'action'	 => 'popinNew')) ?>",
			data: "&matricule=" + id + "&prefix=" + prefix + "&pk=" + pk
		    });
		    ajax.success(function(data) {
			$('.rhPopover').hide().html(data).css({
			    'top': py,
			    'left': px
			}).show('normal');
		    });
		    ajax.error(function() {
			alert("Erreur de chargement");
		    });
		    // Détecter clic en dehors du DIV
		    $(document).mouseup(function(e) {
			var container = $(".rhPopover");
			if (container.has(e.target).length === 0) { 			    // Suppression du DIV
			    container.remove();
			}
		    });
		});
	    });
</script>

<style type="text/css">
    pl-table{
        border-collapse: collapse;
    }
    .pl-table tr,
    .pl-table td{
        border: 1px solid #DFDFDF;
        padding: 4px 2px;
    }
    .pl-table thead tr{
        background: #95B3D7;
    }
    .pl-table thead td{
        text-align: center;
        min-height: 1.5em;
    }
    .pl-table tbody tr:nth-child(even){
        background: #f8f8f8;
    }
    .color-jaune1{
        background: #fff67f;
    }
    .color-jaune2{
        background: #ffee05;
    }
    .color-jaune3{
        background: #ffe84c;
    }
    <?php foreach ($codes as $code): ?>
        .color-<?php echo $code->code ?>{ background: <?php echo $code->couleur ?>}
    <?php endforeach ?>
    .rhPopover{
	position: absolute;
	z-index: 9999;
	width: 500px;
    }
</style>